//
//  OneOrderViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/28.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "OneOrderViewController.h"
#import "Order.h"
#import "FaceToFaceTableViewCell.h"
#import "OrderTableViewCell.h"
#import "PersonalDetailsViewController.h"
#import "OrderDetailsViewController.h"
#import "OrderApi.h"
#import "RefusedViewController.h"


@interface OneOrderViewController ()

@end

@implementation OneOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.bounces = YES;
    [self addMj_header];
    [self loadData];
    
    self.title = @"订单";
    [self.tableView registerCellWithNib:@"OrderTableViewCell"];
    [self.tableView registerCellWithNib:@"FaceToFaceTableViewCell"];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadData
{
    WeakSelf(self)
    OrderApi *api = [[OrderApi alloc]initRequestShow:self.orderID];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            [weakself.dataArray removeAllObjects];
            
            Order *order = [Order new];
            [order setValuesForKeysWithDictionary:[request.responseJSONObject objectForKey:@"data"]];
            [weakself.dataArray addObject:order];
        }
        
        [weakself.tableView reloadData];
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Order *order = self.dataArray[indexPath.row];
    int type = order.type;
    WeakSelf(self)
    if ((type == mianduimian_OrderType && !order.is_operationOrder) || (type == mianduimian_OrderType && order.refused_at)) {
        FaceToFaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FaceToFaceTableViewCell" forIndexPath:indexPath];
        [cell setOrder:order];
        cell.describe.spreadText = ^(id obj) {
            [CALayer cancelAnimation:^{
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
            }];
        };
        
        cell.acceptBlock = ^(id obj) {
            OrderApi *api = [[OrderApi alloc]initRequestAccept:order.id];
            [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                if (kCode(request.responseJSONObject)) {
                    [Tool SendMessage_AcceptOrder:order.id hx_username:order.user_hx];
                    [weakself loadData];
                }
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                
            }];
        };
        
        cell.refuseBlock = ^(id obj) {
            RefusedViewController *vc = [[RefusedViewController alloc]init];
            vc.order = order;
            [weakself pushViewController:vc animated:YES];
            
        };
        
        return cell;
        
    }else{
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTableViewCell" forIndexPath:indexPath];
        
        if (type == mianduimian_OrderType) {
            if (order.is_operationOrder) {
                cell.time.hidden = NO;
                cell.time_H.constant = 17;
                cell.line_Top.constant = 8;
                NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:@"预约时间: " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
                
                [attString appendAttributedString:[[NSAttributedString alloc] initWithString:order.serviced_at attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:k666666}]];
                cell.time.attributedText = attString;
                
            }else{
                cell.time.hidden = YES;
                cell.time_H.constant = 0;
                cell.line_Top.constant = -4;
            }
        }else{
            cell.time.hidden = YES;
            cell.time_H.constant = 0;
            cell.line_Top.constant = -4;
        }
        
        [cell setOrder:order type:type];
        
        cell.touchHeadPortrait = ^(id obj) {
            [weakself pushViewController:[PersonalDetailsViewController PersonalDetailsViewController:order UserID:order.user_id] animated:YES];
        };
        
        cell.touchCheckDetails = ^(id obj) {
            [weakself pushViewController:[OrderDetailsViewController OrderDetailsViewController:order] animated:YES];
        };
        
        WeakSelf(cell)
        cell.touchCurriculumVitae = ^(id obj) {
            if (order.allow_check){
                //已同意去查看
                PersonalDetailsViewController *vc = [PersonalDetailsViewController PersonalDetailsViewController:order UserID:order.user_id];
                vc.isRecords = YES;
                [weakself pushViewController:vc animated:YES];
            }else{
                //未同意去申请
                OrderApi *api = [[OrderApi alloc] initRequestSendApplyForWithOrderID:order.id];
                api.needHUD = NO;
                [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                    
                    if (kCode(request.responseJSONObject)) {
                        [MBProgressHUD showSuccess:@"已发送申请"];
                        order.allow_send_check = NO;
                        weakcell.applyForBtn.enabled = NO;
                        
                        [Tool SendMessage_ApplyForDiagnosisRecord:order.id hx_username:order.user_hx];
                    }else{
                        [MBProgressHUD showError:@"申请发送失败"];
                    }
                    
                } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                    
                }];
            }
        };
        
        cell.describe.spreadText = ^(id obj) {
            [CALayer cancelAnimation:^{
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
            }];
        };
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Order *order = self.dataArray[indexPath.row];
    int type = order.type;
 
    if ((type == mianduimian_OrderType && !order.is_operationOrder) || (type == mianduimian_OrderType && order.refused_at)) {
        if (order.is_timeout || order.refused_at) {
            return 118 + [order cellHeight];
        }else{
            return 155 + [order cellHeight];
        }
    }else{
        if (type == mianduimian_OrderType) {
            if (order.is_operationOrder) {
                return 210 + [order cellHeight];
            }
        }
    }
    return 178 + [order cellHeight];
}

@end
